package com.tuanzi.loan.web.bean.statistics.api;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.product.product.LoanApiInfo;
import com.tuanzi.loan.business.service.product.product.LoanApiInfoService;
import com.tuanzi.loan.business.service.statistics.ActivityParticipateStatService;
import com.tuanzi.loan.business.service.statistics.ApiFlowStatService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.statistics.api.ApiFlowStatSearchRequest;
import com.tuanzi.loan.vo.response.statistics.api.ApiFlowStatSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.statistics.api.ApiFlowStatDataModel;
import com.tuanzi.server.loan_base.model.bean.common.CommUserType;
import com.tuanzi.server.loan_base.model.db.CommProductChannel;
import com.tuanzi.server.loan_base.model.db.CommProductPrdid;
import com.tuanzi.server.loan_base.model.db.LoanProductInfo;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.joda.time.LocalDate;
import org.primefaces.model.StreamedContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 活动参与统计
 *
 * @author JinRuiyang 2018/1/26 10:34:21
 */
@Debug
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class StatApiFlowBean extends JSFListingBean<ApiFlowStatSearchRequest, ApiFlowStatSearchResponse> {

    @Autowired
    private LoanApiInfoService loanApiInfoService;
    @Autowired
    private ApiFlowStatService apiFlowStatService;

    private StreamedContent xlsFile;

    private List<LoanApiInfo> apiList;

    private List<ApiFlowStatSearchResponse> apiFlowStatSearchResponses;

    @PostConstruct
    private void init() {
        initLazyDataModel();
        initList();
    }

    private void initList() {
        apiList = loanApiInfoService.findAll();
    }


    public void onReset() {
        initLazyDataModel();
    }

    public void onRefresh() {
        lazyDataModel = new ApiFlowStatDataModel($);
        addSuccessfullyMessage();
    }

    private void initLazyDataModel() {
        $ = new ApiFlowStatSearchRequest();
        // 初始化默认当前日期
        LocalDate now = LocalDate.now();
        LocalDate before = now.plusDays(-1);
        $.setStartTime(before.toDate());
        $.setEndTime(now.toDate());
        lazyDataModel = new ApiFlowStatDataModel($);
    }

    public void onAutoSearch() {
        lazyDataModel = new ApiFlowStatDataModel($);
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(DictionaryCode.API_FLOW_PROD);
    }

}




